#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include<string.h>

typedef struct
{
    char name[20];
    int age;
    char addr[64];

} STU;

int main(int argc, char **argv)
{

    FILE *output = fopen("fwrite2.data", "w+");
    if (output == NULL)
    {
        perror("file is write error");
    }
    STU stu;
    for (size_t i = 0; i < 10; i++)
    {
        sprintf(stu.name,"naame-%ld",i);
        stu.age = 10+i;
        sprintf(stu.addr,"myaddr-%ld",i);
        fwrite(&stu,sizeof(stu),1,output);
    }
    //這里一定要加上，不然下面的讀取數據就會發生問題
    fclose(output);
    memset(&stu,0,sizeof(stu));
    // stu = { 0 }; //error
    

    output = fopen("fwrite2.data","r+");
    if (output == NULL)
    {
        perror("file is read error");
    }
    fseek(output,1*sizeof(stu),SEEK_SET);
    fread(&stu,sizeof(stu),1,output);
    printf("name=%s, age=%d, addr=%s \n",stu.name,stu.age,stu.addr);

    fclose(output);
    

    return 0;
}